import axios from '@/axios.config'

export default {
    state: {
        sessionId: '',
        msg: '',
        pageType: "",
        userName: "",
        userType: "",
        retCode: 1
    },
    mutations: {
        save_session(state, payload) {
            state.sessionId = payload.session_id,
            state.pageType = payload.save_pageType,
            state.userName = payload.save_userName,
            state.userType = payload.save_userType,
            state.retCode = payload._retcode
        },
        send_msg(state, payload) {
            state.msg = payload._msg
        },
        send_msg2(state, payload) {
            state.msg = payload._msg,
            state.retCode = payload._retcode
        },
    },
    actions: {
        login({ commit }, payload) {
            return axios.post('/api/auth/user/login',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('save_session', {
                            session_id: res.data.sessionId,
                            save_pageType: res.data.data.pageType,
                            save_userName: res.data.data.loginName,
                            save_userType: res.data.data.userType,
                            _retcode: 0
                        })
                    }else{
                        commit('send_msg2', {
                            _msg: res.data.retMsg,
                            _retcode: 1
                        })
                    }
                })
                .catch((error) => {
                    console.log(error);
                })
        }
    }
}